/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.dbg.view.content {
	import com.googlecode.milib.dbg.view.content.AbstractMLDBGWindowContent;
	import com.googlecode.milib.util.MLBitmapUtil;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.IBitmapDrawable;
	import flash.display.MovieClip;

	/**
	 * @author Marek Brun
	 */
	public class MLDBGBitmapDrawableView extends AbstractMLDBGWindowContent {

		private var model:IBitmapDrawable;
		private var bitmap:Bitmap;

		public function MLDBGBitmapDrawableView(name:String) {
			super(new MovieClip(), name);
			bitmap=new Bitmap();
			mlmc.mc.addChild(bitmap);
		}
		
		override public function getIsScrollable():Boolean {
			return false;
		}
		
		override protected function drawData():void {
			if(model is BitmapData) {
				bitmap.bitmapData=BitmapData(model);
			}else{
				if(bitmap.bitmapData){
					bitmap.bitmapData.dispose();
				}
				var bd:BitmapData;
				if(model is DisplayObject){
					if(bitmap.bitmapData){
						bitmap.bitmapData.dispose();
					}
					bitmap.bitmapData=MLBitmapUtil.getBitmapDataByDisplay(model as DisplayObject);
				}else{
					bd=new BitmapData(width, height);
					bd.draw(model);
					bitmap.bitmapData=bd;
				}
			}
		}
		
		override protected function doSize():void {
			bitmap.x=Math.max(0, width/2-bitmap.width/2);			bitmap.y=Math.max(0, height/2-bitmap.height/2);
		}
		
		public function setModel(model:IBitmapDrawable):void {
			this.model=model;
			if(isEnabled){
				drawData();
			}
		}
		
	}
}
